package io.mvvm.nio.channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @program: Netty
 * @description: transferFrom 文件拷贝
 * @author: 潘
 * @create: 2021-01-17 21:15
 **/
public class FileChannelTransferFromExample {
    public static void main(String[] args) throws Exception {
        // 创建流
        FileInputStream inputStream = new FileInputStream("E:\\work\\Netty\\nio\\src\\main\\resources\\fileName.txt");
        FileOutputStream outputStream = new FileOutputStream("E:\\work\\Netty\\nio\\src\\main\\resources\\fileName1.txt");
        // 获取对应流的 channel
        FileChannel inputStreamChannel = inputStream.getChannel();
        FileChannel outputStreamChannel = outputStream.getChannel();
        // 使用 transferFrom 进行拷贝
        outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());
        // 关闭资源
        inputStreamChannel.close();
        outputStreamChannel.close();
        outputStream.close();
        inputStream.close();
    }
}
